package de.mmis.core.base;

import de.mmis.core.base.DeserializableAnnotations.ConstructorDeserializable;
import de.mmis.core.base.DeserializableAnnotations.DeserializationConstructor;
import de.mmis.core.base.MethodDescriptor.P;
import de.mmis.core.base.Type.TypeName;

/**
 * This class represents a pair of two other objects.
 * 
 * @author Sebastian Bader
 * 
 * @param <F>
 *            the first object type
 * @param <S>
 *            the second object type
 */
@TypeName("pair")
@ConstructorDeserializable(typeIdentifier = "pair")
public class Pair<F, S> {
	private F first;
	private S second;

	/**
	 * Constructor. Generates an instance of this pair using the first and
	 * second given argument and type.
	 * 
	 * @param first
	 *            the first object to store
	 * @param second
	 *            the second object to store
	 */
	@DeserializationConstructor
	public Pair(@P("First") F first, @P("Second") S second) {
		this.first = first;
		this.second = second;
	}

	/**
	 * Gets the first object.
	 * 
	 * @return the first object of this pair
	 */
	public F getFirst() {
		return this.first;
	}

	/**
	 * Sets the first object.
	 * 
	 * @param first
	 *            the first object of this pair.
	 */
	public void setFirst(F first) {
		this.first = first;
	}

	/**
	 * Gets the second object.
	 * 
	 * @return the second object of this pair
	 */
	public S getSecond() {
		return this.second;
	}

	/**
	 * Sets the second object.
	 * 
	 * @param second
	 *            the second object of this pair
	 */
	public void setSecond(S second) {
		this.second = second;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((this.first == null) ? 0 : this.first.hashCode());
		result = prime * result
				+ ((this.second == null) ? 0 : this.second.hashCode());
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Pair<F, S> other = (Pair<F, S>) obj;
		if (this.first == null) {
			if (other.first != null)
				return false;
		} else if (!this.first.equals(other.first))
			return false;
		if (this.second == null) {
			if (other.second != null)
				return false;
		} else if (!this.second.equals(other.second))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "<" + ((this.first == null) ? "null" : this.first.toString())
				+ " , "
				+ ((this.second == null) ? "null" : this.second.toString())
				+ ">";
	}

}
